import { sheets } from '@/app/grid/controller/Sheets';
import { convertReactColorToString } from '@/app/helpers/convertColor';
import type { CellAlign, CellFormatSummary, CellVerticalAlign, CellWrap } from '@/app/quadratic-core-types';
import type { ColorResult } from '@/app/ui/components/ColorPicker';
import { quadraticCore } from '@/app/web-workers/quadraticCore/quadraticCore';
import { DEFAULT_FONT_SIZE, MAX_FONT_SIZE, MIN_FONT_SIZE } from '@/shared/constants/gridConstants';

export const setFillColor = (color?: ColorResult) => {
  quadraticCore.setFillColor(sheets.getRustSelection(), color ? convertReactColorToString(color) : undefined, false);
};

export const clearFillColor = () => {
  quadraticCore.setFillColor(sheets.getRustSelection(), 'blank', false);
};

export const setBold = () => {
  quadraticCore.setBold(sheets.getRustSelection(), undefined, false);
};

export const setItalic = () => {
  quadraticCore.setItalic(sheets.getRustSelection(), undefined, false);
};

export const setTextColor = (rgb?: ColorResult) => {
  quadraticCore.setTextColor(sheets.getRustSelection(), rgb ? convertReactColorToString(rgb) : undefined, false);
};

export const setUnderline = () => {
  quadraticCore.setUnderline(sheets.getRustSelection(), undefined, false);
};

export const setStrikeThrough = () => {
  quadraticCore.setStrikeThrough(sheets.getRustSelection(), undefined, false);
};

export const setAlign = (align: CellAlign) => {
  quadraticCore.setAlign(sheets.getRustSelection(), align, false);
};

export const setVerticalAlign = (verticalAlign: CellVerticalAlign) => {
  quadraticCore.setVerticalAlign(sheets.getRustSelection(), verticalAlign, false);
};

export const setWrap = (wrap: CellWrap) => {
  quadraticCore.setWrap(sheets.getRustSelection(), wrap, false);
};

export const textFormatIncreaseDecimalPlaces = () => {
  quadraticCore.changeDecimalPlaces(sheets.getRustSelection(), 1, false);
};

export const textFormatDecreaseDecimalPlaces = () => {
  quadraticCore.changeDecimalPlaces(sheets.getRustSelection(), -1, false);
};

export const setCellCommas = () => {
  quadraticCore.setCommas(sheets.getRustSelection(), undefined, false);
};

export const textFormatSetCurrency = (currency = '$') => {
  quadraticCore.setCellCurrency(sheets.getRustSelection(), currency, false);
};

export const textFormatSetPercentage = () => {
  quadraticCore.setCellPercentage(sheets.getRustSelection(), false);
};

export const removeNumericFormat = () => {
  quadraticCore.removeNumericFormat(sheets.getRustSelection(), false);
};

export const textFormatSetExponential = () => {
  quadraticCore.setCellExponential(sheets.getRustSelection(), false);
};

export const clearFormatting = () => {
  quadraticCore.clearFormatting(sheets.getRustSelection(), false);
};

export const clearFormattingAndBorders = () => {
  clearFormatting();
};

export const setFontSize = (fontSize: number) => {
  quadraticCore.setFontSize(sheets.getRustSelection(), fontSize, false);
};

export const increaseFontSize = async () => {
  const formatSummary = await quadraticCore.getFormatSelection(sheets.sheet.cursor.save());
  if (formatSummary && 'fontSize' in formatSummary) {
    const currentSize = (formatSummary as CellFormatSummary).fontSize ?? DEFAULT_FONT_SIZE;
    const newSize = Math.min(currentSize + 1, MAX_FONT_SIZE);
    setFontSize(newSize);
  } else {
    setFontSize(DEFAULT_FONT_SIZE + 1);
  }
};

export const decreaseFontSize = async () => {
  const formatSummary = await quadraticCore.getFormatSelection(sheets.sheet.cursor.save());
  if (formatSummary && 'fontSize' in formatSummary) {
    const currentSize = (formatSummary as CellFormatSummary).fontSize ?? DEFAULT_FONT_SIZE;
    const newSize = Math.max(currentSize - 1, MIN_FONT_SIZE);
    setFontSize(newSize);
  } else {
    setFontSize(DEFAULT_FONT_SIZE - 1);
  }
};
